﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Common;
using RuleCode.Table;

namespace map.search
{
    public partial class area : System.Web.UI.Page
    {
        /// <summary>
        /// 处理激活脚本
        /// </summary>
        private void OnInit2()
        {
            if ((Request.Params["keyvalue"] != null) && (Request.Params["area4recid"] != null))
            {
                string scriptTag =
                    "<script type=\"text/javascript\">initsdsfd(";
                scriptTag += "'" + Request.Params["keyvalue"].ToString() + "'" + ",";
                scriptTag += "'" + Request.Params["area4recid"].ToString() + "'";

                scriptTag += ");</script>";

                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                        "messagew22", scriptTag, false);
            }
            else
            {
                string scriptTag =
                    "<script type=\"text/javascript\">initsdsfd(";
                scriptTag += "'" + "'" + ",";
                scriptTag += "'" + "'";

                scriptTag += ");</script>";

                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                        "messagew22", scriptTag, false);
            }



        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Literal_map.Text = RuleCode.Util.getmapurl();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxFunction));
            if (!IsPostBack)
            {
                OnInit2();
                bind1();
                BindMainWebPager(!string.IsNullOrEmpty(Request.Params["keyvalue"]));


            }
        }


        /// <summary>
        /// 绑定数据源
        /// </summary>
        protected void BindMainWebPager(bool useSearchKeys)
        {

            /*EXEC	@return_value = [dbo].[searchtextitem]
		@keyword = N'1',
		@keywordtext = N'222',
		@provice = ,
		@area = area3recid,
		@pricefrom = NULL,
		@priceto = NULL,
		@rsms = NULL,
		@qtth = NULL,
		@stwx = NULL,
		@sdfh = NULL,
		@cataid = NULL
             */




            string sqlText = "exec [searchtextarea4] ";
            if (useSearchKeys)
            {
                sqlText += " '" + zfunction.ReplaceKeyWord(Request["keyvalue"]) + "'";
                sqlText += ",  '" + Request["keyvalue"] + "'";

            }
            else
            {
                sqlText += " '" + zfunction.ReplaceKeyWord("") + "'";
                sqlText += ",  '" + "" + "'";

            }


            sqlText += ", '" + zfunction.Province + "'";
            string area = "','";
            //sqlText += ", @priceto=" + Request["highPrice"];
            if (Request.Params["area4recid"] != null)
            {

                if (Request.Params["area4recid"] == "undefined")
                {
                    area = "','";

                }
                else
                {
                    RuleCode.Table.区域 district = 区域.Find("recid= '" + Request.Params["area4recid"] + "' ");


                    area = district["区编码"].ToString() + "','" + district["区域编码"];
                }
            }
            sqlText += ",'" + area + "'";
            //sqlText += ", @qtth='" + Request["qtthValue"] + "'";
            //sqlText += ", @stwx='" + Request["shwxValue"] + "'";
            //sqlText += ", @sdfh='" + Request["sdfhValue"] + "'";
            //sqlText += ", @cataid='" + Request["currentCate"] + "'";
            //sqlText += ",0,0,0,0,'" + "" + "'";

            DataSet dSet = SqlHelper.Instance.ExecuteQuery(sqlText);
            if (dSet.Tables.Count > 0)
            {
                int count = dSet.Tables[0].Rows.Count;
                this.TotalCountLabel.Text = count.ToString() + " 个地标符合要求";


                bool hasData = (count > 0);
                this.NoDataDiv.Visible = !hasData;

                //this.MainDataList.DataSource = dSet.Tables[0];
                this.WebPager1.DataSource = dSet.Tables[0];
                //this.MainDataList.DataBind();
                WebPager1.DataBind();
            }
        }
      
        protected void DataList_all_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
           // string id = Request.Params["id"];
            DataTable dt;
            string sql = "";
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //string _p1 = ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString();
                //string _p2 = ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString();
                //string _p3 = ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[2].ToString();
                //string _p4 = ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[3].ToString();
                string area = ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[10].ToString();

               // sql = " select * from vshop where p1 = '" + _p1 + "' and p2 = '" + _p2 + "' and "
                   // + "areaRecid=" + id;
                sql = "select   count(a.cbuserid)as ShopCount from cbuser as a, cbshop as b where a.cbuserid=b.cbuserid and a.cbuserbizarea='"+area+"' and b.cbstatus='启用'";
                dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                if (dt != null)
                {

                    DataList DataList_in = e.Item.FindControl("list_in") as DataList;
                    DataList_in.DataSource = dt;
                    DataList_in.DataBind();
                }
            }
        }


        private void bind1()
        {

            Literal1.Text = chatblog.Util.bind1();
        }

        protected void WebPager1_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            this.WebPager1.CurrentPageIndex = e.NewPageIndex;
            BindMainWebPager(false);
        }
    }
}
